import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/IndexPage.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/common/HomePage.vue'),
    redirect: '/home/view-projects',
    meta: { requiresAuth: true },
    children: [
      // 公共子路由
      {
        path: 'view-projects',
        name: 'viewProjects',
        component: () => import('@/views/common/ViewProjects.vue')
      },
      {
        path: '/project-details',
        name: 'project-details',
        component: () => import('@/views/common/ProjectDetailPage.vue')
      },

      // 教师专用子路由
      {
        path: 'statistic',
        name:'statistic',
        component: () => import('@/views/teacher/StatisticPage.vue'),
        meta: { role: 'teacher' }
      },

      {
        path: 'student-management',
        name: 'studentManagement',
        component: () => import('@/views/teacher/StudentManagement.vue'),
        meta: { role: 'teacher' }
      },
      {
        path: 'review-join-project',
        name: 'reviewJoinProject',
        component: () => import('@/views/teacher/ReviewJoinProject.vue'),
        meta: { role: 'teacher' }
      },
      {
        path: 'review-create-project',
        name: 'reviewCreateProject',
        component: () => import('@/views/teacher/ReviewCreateProject.vue'),
        meta: { role: 'teacher' }
      },
      {
        path: 'create-project',
        name: 'createProject',
        component: () => import('@/views/teacher/CreateProject.vue'),
        meta: { role: 'teacher' }
      },
      {
        path: 'account-management',
        name: 'accountManagement',
        component: () => import('@/views/teacher/AccountManagement.vue'),
        meta: { role: 'teacher' }
      },
      // 学生专用子路由
      {
        path: 'apply-project',
        name: 'applyProject',
        component: () => import('@/views/student/ApplyProject.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'UpdateMyInfo',
        name: 'updateMyInfo',
        component: () => import('@/views/student/UpdateMyInfo.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/pending-application',
        name: 'pendingApplication',
        component: () => import('@/views/student/PendingApplication.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/pending-review',
        name: 'pendingReview',
        component: () => import('@/views/student/PendingReview.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/rejected',
        name: 'rejected',
        component: () => import('@/views/student/Rejected.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/ongoing',
        name: 'ongoing',
        component: () => import('@/views/student/Ongoing.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/created',
        name: 'created',
        component: () => import('@/views/student/Created.vue'),
        meta: { role: 'student' }
      },
      {
        path: 'project-management/approved',
        name: 'approved',
        component: () => import('@/views/student/Approved.vue'),
        meta: { role: 'student' }
      },



    ]
  },
  // {
  //   path: '/unauthorized',
  //   name: 'unauthorized',
  //   component: () => import('@/views/common/Unauthorized.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const userRole = userStore.userInfo

  if (to.meta.requiresAuth && !userRole) {
    console.log('用户未登录，重定向到首页')
    next('/')
  } else if (to.meta.role && to.meta.role !== userRole) {
    console.log('角色不匹配，重定向到未授权页面')
    next('/unauthorized')
  } else {
    next()
  }
})

export default router
